#include <iostream>
#include <algorithm>

using namespace std;

const int N = 1e5 + 10;

typedef long long LL;

int p[N];

int n, m, k;

LL res;

struct Edge
{
	int a, b, w;
	
	bool operator< (const Edge &t)const
	{
		return w > t.w;
	}
}edge[N];

void init()
{
	for (int i = 1; i <= n; i ++ ) p[i] = i;
}

int find(int x)
{
	if (p[x] != x) p[x] = find(p[x]);
	
	return p[x];
}

void kruskal()
{
	sort(edge, edge + m);
	
	int cnt = 0;
	
	for (int i = 0; i < m; i ++ )
	{
		if (cnt == k) break;
		auto e = edge[i];
		int a = e.a, b = e.b, w = e.w;
		
		int pa = find(a), pb = find(b);
		
		if (pa == pb) continue;
		
		p[pa] = pb;
		cnt ++ ;
		res += w;
	}
}

int main()
{
	cin >> n >> m >> k;
	
	init();
	
	for (int i = 0; i < m; i ++ )
	{
		int a, b, w;
		cin >> a >> b >> w;
		
		edge[i] = {a, b, w};
	}
	
	kruskal();
	
	cout << res << endl;
	
	return 0;
}